package labuladong.第二章.反转链表;

import leetcode.Head;
import leetcode.ListNode;

import java.util.List;

/**
 * 反转链表前n个结点
 */
public class Demo02 {
    static ListNode successor = null;
    public static ListNode reverse(ListNode head, int n) {
        if (n==1) {
            successor = head.next;
            return head;
        }
        ListNode last = reverse(head.next, n-1);
        head.next.next = head;
        head.next = successor;
        return last;
    }

    public static void main(String[] args) {
        ListNode head = reverse(Head.genListEven(), 3);
        Head.printList(head);
    }
}
